package com.project.web.order.controller.app.webNative;

import com.github.pagehelper.PageInfo;
import com.project.common.core.utils.exception.Result;
import com.project.web.order.controller.common.BaseController;
import com.project.web.order.model.app.Product;
import com.project.web.order.service.app.ProductService;
import com.project.web.util.exception.ORDER_RESP_CODE_ENUM;
import com.project.web.util.exception.OrderCustomException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Web 商品API
 *
 * @Author: wyy
 * @Date: 2021-02-02 02:24
 */
@Api(tags = "【Web】Web商品API")
@RestController("webProductController")
@RequestMapping("/order/auth/web")
public class ProductController extends BaseController {

    @Resource(name = "productServiceImpl")
    private ProductService productService;

    /**
     * 分页查询商品
     *
     * @param product  商品对象
     * @param pageNum  页码
     * @param pageSize 每页条数
     * @return
     */
    @ApiOperation(value = "分页查询商品")
    @GetMapping(value = "/selectOfPage")
    public Result<PageInfo<Product>> selectOfPage(Product product, Integer pageNum, Integer pageSize) {
        // 初始化分页参数
        if (pageNum == null || pageSize == null) {
            throw new OrderCustomException(ORDER_RESP_CODE_ENUM.MISS_PARAMETER);
        }
        PageInfo<Product> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);

        // 查询商品列表
        PageInfo<Product> productPageInfo = productService.selectList(product, pageInfo);

        return getResult(productPageInfo);
    }

    /**
     * 保存商品
     *
     * @param product 商品对象
     * @return
     */
    @ApiOperation(value = "保存商品")
    @PostMapping(value = "/save")
    public Result<Object> save(@RequestBody Product product) {
        // 保存商品
        productService.save(product);
        return getResult(null);
    }

    /**
     * 更新商品
     *
     * @param product 更新商品
     * @return
     */
    @ApiOperation(value = "保存商品")
    @PostMapping(value = "/update")
    public Result<Object> update(@RequestBody Product product) {
        // 更新商品
        productService.update(product);
        return getResult(null);
    }

    /**
     * 根据商品Id查询商品
     *
     * @param productId 商品Id
     * @return
     */
    @ApiOperation(value = "根据商品Id查询商品")
    @GetMapping(value = "/getById")
    public Result<Product> getByProductId(Long productId) {
        // 初始化分页参数
        if (productId == null) {
            throw new OrderCustomException(ORDER_RESP_CODE_ENUM.MISS_PARAMETER);
        }
        Product product = productService.getById(productId);
        return getResult(product);
    }

    /**
     * 查询所有已上架商品
     *
     * @param
     * @return
     */
    @ApiOperation(value = "查询所有已上架商品")
    @GetMapping(value = "/getProductsOfNormal")
    public Result<List<Product>> getProductsOfNormal() {
        // 查询商品列表
        List<Product> productsOfNormal = productService.getProductsOfNormal();
        return getResult(productsOfNormal);
    }

}
